package com.sjgtw.cloud.system.utils;

import java.util.Collection;
import java.util.List;
import com.sjgtw.cloud.common.core.constant.Constants;
import com.sjgtw.cloud.common.core.utils.SpringUtils;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.domain.SysDictData;

/**
 * <p>Title: 数据字典工具类</p>
 * <p>Description: </p>
 * @author Admin
 * @date 2020-7-4
 */
public class DictUtils
{
	/**
	 * <p>Title: 字典数据缓存处理</p>
	 * <p>Description: </p>
	 * @param key       参数键
	 * @param dictDatas 字典数据列表
	 */
    public static void setDictCache(String key, List<SysDictData> dictDatas)
    {
        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas);
    }

	/**
	 * <p>Title: 获取缓存中的字典数据</p>
	 * <p>Description: </p>
	 * @param key 参数键
	 * @return dictDatas 字典数据列表
	 */
    public static List<SysDictData> getDictCache(String key)
    {
        Object cacheObj = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(cacheObj))
        {
            List<SysDictData> dictDatas = StringUtils.cast(cacheObj);
            return dictDatas;
        }
        return null;
    }

	/**
	 * <p>Title: 清空字典缓存</p>
	 * <p>Description: </p>
	 */
    public static void clearDictCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisService.class).keys(Constants.SYS_DICT_KEY + "*");
        SpringUtils.getBean(RedisService.class).deleteObject(keys);
    }

    /**
	 * <p>Title: 设置字典缓存主键</p>
	 * <p>Description: </p>
	 * @param configKey 参数键
	 * @return 缓存键key
	 */
    public static String getCacheKey(String configKey)
    {
        return Constants.SYS_DICT_KEY + configKey;
    }
}
